Skip to content

Comments

Add resource_name variable to template context#9

Merged
jeffreyaven merged 1 commit intomainfrom
claude/resource-name-template-access-9Zz51
Feb 23, 2026
Merged

Add resource_name variable to template context#9
jeffreyaven merged 1 commit intomainfrom
claude/resource-name-template-access-9Zz51

Conversation

@jeffreyaven
Copy link
Contributor

Summary

This change enables templates to reference the current resource's name via a resource_name variable, similar to existing stack_name and stack_env variables. This allows global context values (like tags) to be customized per-resource using template expressions.

Key Changes

  • Added resource_name injection: The get_full_context() function now injects resource_name into the context before rendering resource properties
  • Implemented deferred variable resolution: New re_render_context_with_deferred_vars() function re-renders global context values that contain template expressions (e.g., {{ resource_name }}), allowing globals defined at startup to reference resource-specific variables
  • Updated context flow: Global context is now re-rendered with the resource name available, ensuring that deferred template expressions in globals are properly resolved before being merged with resource properties
  • Added comprehensive tests: Six new test cases covering:
    • resource_name availability in full context
    • resource_name usage in resource properties
    • Resolution of deferred globals containing {{ resource_name }}
    • Per-resource variation of resource_name
    • No-op behavior when no templates are present
    • Deferred variable resolution in context

Implementation Details

  • The resource_name variable is injected into a cloned copy of the global context before property rendering
  • Deferred variables are identified by checking for {{ in values and are re-rendered using the template engine
  • Boolean values are normalized (Python's True/False → JSON's true/false) during re-rendering
  • Failed re-renders log a debug warning but don't break the flow
  • All existing context variables remain available and unchanged

https://claude.ai/code/session_01JVr4F97rZNWMEsADMpAotd

Introduces {{ resource_name }} as a JIT-evaluated template variable
(alongside stack_name and stack_env) that resolves to the current
resource's name during processing. This enables tagging resources
with their own name in both resource-level props and globals like
global_tags.

Global values containing {{ resource_name }} are preserved as literal
template expressions during initial rendering (since no resource is
known yet), then re-rendered per-resource in get_full_context() when
the resource name becomes available.

https://claude.ai/code/session_01JVr4F97rZNWMEsADMpAotd
@jeffreyaven jeffreyaven merged commit ccd51eb into main Feb 23, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants